package clientGUI;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.*;

import clientApp.VirtualAdvisor;
import clientGUI.studentGUI.GUI_Function_Library;
import clientGUI.studentGUI.RegisterGUI;

public class LoginGUI {

	private JFrame loginFrame;
	private boolean ready;
	private String username;
	private VirtualAdvisor myVA;
	
	public LoginGUI(VirtualAdvisor VA)
	{
		ready = false;
		username = "";
		myVA = VA;
		JPanel loginPane = createPaneLOGIN();
		loginFrame = GUI_Function_Library.displayPane ("Log In", loginPane, true);
	}
	

	public boolean ready()
	{
		return ready;
	}

	/*
	 * register student
	 * TALK TO DATABASE
	 */
	private void registerStudent() throws InterruptedException, IOException
	{
		// display register screen - do not close login screen
		RegisterGUI register = new RegisterGUI(myVA);
		
		// send courses to database
	}

	public String username()
	{
		return username;
	}
	
	/**
	 * login to account
	 * QUERY DATABASE NEEDED
	 * @param username
	 * @param password
	 * @throws IOException 
	 */
	private void checkLogin(String username, String password) throws Exception
	{
//		System.out.println("LOGINGUI: Login: " + username + " " + password);
		if(username == null || password == null || username.isEmpty() || password.isEmpty())
			throw new Exception("Either Username or Password is Blank. Fix it");
		boolean isUser = myVA.checkUser(username, password);
		// query database - okay?
		if(isUser)
		{
			this.username = username;
			myVA.userLogin(myVA.getLastUser(), myVA.lastAccessRights());
			GUI_Function_Library.undisplayFrame(loginFrame);
			ready = true;
		}
		// try logging in again
		else
			throw new Exception("Invalid Login. LoginGUI");
	}

	/*
	 * create login screen
	 */
	private JPanel createPaneLOGIN()
	{
		// query username
		JPanel usernamePane			= new JPanel (new GridLayout(1,2));
		JLabel usernameLabel		= new JLabel ("Username:");
		final JTextField usernameField	= new JTextField(10);
		usernamePane.add(usernameLabel);
		usernamePane.add(usernameField);

		// query password
		JPanel passwordPane			= new JPanel(new GridLayout(1,2));
		JLabel passwordLabel		= new JLabel ("Password:");
		final JPasswordField passwordField = new JPasswordField(10);
		passwordPane.add(passwordLabel);
		passwordPane.add(passwordField);

		// buttons
		JButton registerButton		= new JButton ("Register");
		JButton loginButton			= new JButton ("Log In");
		JPanel	buttonPane			= new JPanel();
		buttonPane.add(registerButton);
		buttonPane.add(loginButton);

		// add meaning to buttons
		registerButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try{
					registerStudent();
				}catch(Exception e2)
				{
					GUI_Function_Library.exceptionHandler("LoginGUI", e2);
				}
			}
		});
		loginButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				String password = "";
				for(char c: passwordField.getPassword())
					password += c;

				try {
					checkLogin(usernameField.getText(), password);
				} catch (Exception e1) {
					GUI_Function_Library.exceptionHandler("LoginGUI", e1);
					GUI_Function_Library.exceptionHandler(e1);
					
					// TODO Auto-generated catch block
//					e1.printStackTrace();
				}
				
			}
		});


		//----- put it all together
		JPanel loginPane			= new JPanel(new GridLayout(3,1));
		loginPane.add(usernamePane);
		loginPane.add(passwordPane);
		loginPane.add(buttonPane);

		// return it
		return loginPane;
	}

}
